/*
* @Author: ystlong
* @Date:   2018-05-12 10:21:41
* @Last Modified by:   ystlong
* @Last Modified time: 2018-05-12 11:23:50
*/

#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <limits.h>
#include <string.h>
#include <unistd.h>

size_t get_executable_path( char* processdir,char* processname, size_t len)
{
	char* path_end;
	if(readlink("/proc/self/exe", processdir,len) <=0)
		return -1;
	path_end = strrchr(processdir, '/');
	if(path_end == NULL)
		return -1;
	++path_end;
	strcpy(processname, path_end);
	*path_end = '\0';
	return (size_t)(path_end - processdir);
}

void (*realexit)(int);

void exit(int status)
{
	void *handle;
	char *error;
	char path[PATH_MAX];
	char processname[1024];
	get_executable_path(path, processname, sizeof(path));
	if (strcmp(processname, "armlmd") == 0) {
		puts("=========got armlmd: sleep 10s=====");
		int i;
		for(i=0;i<50000;i++)
			sleep(10);
		return;
	}
	puts("-----exit hold-----");
	handle = dlopen("libc.so.6", RTLD_LAZY);
	if ((error = dlerror()) != NULL){
		puts(error);
		_exit(-1);
	}
	realexit = dlsym(handle, "_exit");

	if ((error = dlerror()) != NULL){
		puts(error);
		_exit(-1);
	}
	realexit(status);
}

